{% extends "back.html" %}
{% block hello %}
    {% import "fenye.html" as macros %}
    <div class="tpl-content-wrapper " style="margin-top: -7px;">
        <div class="tpl-content-page-title"></div>
        <div class="tpl-portlet-components" style="">
            <div class="portlet-title">
                <div class="caption font-green bold">
                    <span class="am-icon-code"></span> 用例
                </div>
            </div>
            {% with messages = get_flashed_messages() %}
                {% if messages %}
                    <ul class='flashes'>
                        {% for message in messages %}
                            <li style="color: #b92c28;">{{ message }}</li></ul>
                        {% endfor %}
                {% endif %}
            {% endwith %}
            <strong>项目:</strong>
            <select name='project' id="projecttestcase" onclick="getprotest()">
                {% for project in projects %}
                    <option>{{ project.project_name }}</option>
                {% endfor %}
            </select>&nbsp;&nbsp;&nbsp;
            <strong>
                <span>接口的类型：</span>
            </strong>
            <select id="interfacetype" onclick="getprotest()">
                <option>http</option>
                <option>dubbo</option>
            </select>
            <button  class="btn-success">录制case</button>
            <form method="POST" action="{{ url_for('case.duoyongli') }}">
                <div style="margin-top: 10px">
                    <a href="{{ url_for('case.addtestcase') }}">
                        <input type="button" name="" class="am-btn am-btn-default am-btn-success" value="添加用例">
                    </a>
                    <a href="{{ url_for('case.daoru_case') }}">
                        <input type="button" name="" class="am-btn am-btn-default am-btn-success" value="批量导入">
                    </a>
                    <button class="am-btn am-btn-default am-btn-success">批量执行</button>
                    <input type="button" value="导出接口用例" class="btn-default am-btn-default am-btn" onclick="dakai()">
                    <label>
                        <input name="dingding" type="radio" value="email">
                    </label>
                    <strong>发送邮件报告</strong>
                    <label>
                        <input name="dingding" type="radio" value="dingding">
                    </label>
                    <strong>发送钉钉报告</strong>
                    <strong>请选择测试环境：</strong>
                    <select id="ceshice" name="urltest">
                    </select>
                </div>
                <table class="am-table am-table-striped am-table-hover table-main">
                    <caption>
                        <strong>用例</strong>
                    </caption>
                    <thead>
                    <tr id="interfacete">
                    </tr>
                    </thead>
                    <tbody id="testyonglis">
                    </tbody>
                </table>
            </form>
        </div>
    </div>
    <div style="display:none;position:fixed;z-index: 210;width: 800px;top: 100px;left: 20%;box-shadow: 0 0 10px rgba(0,0,0,0.6);border: solid 1px #255c71;border-radius: 5px 5px 0 0;background-color: #9eacb4"
         id="ceshiyongli">
        <div style="height: 5px;">请选择测试用例执行的测试环境</div>
        <div style="background-color: whitesmoke;margin-top: 24px">
            <span style="text-align: center">
                项目：<strong id="ceshiyongprco"></strong>
            </span>
            <span>所属测试环境：</span>
            <select id="xuanzehuanjing">
            </select>
        </div>
        <div style="margin-top: 50%;margin-bottom: auto">
            <input value="执行" type="button" class="btn-default" id="zhixingceshiyongli" onclick="ceshiyongli()">
            <input value="取消" type="button" class=" btn-default" onclick="guanbi()">
        </div>
    </div>
    <div style="display:none;position:fixed;z-index: 210;width: 800px;top: 100px;left: 20%;box-shadow: 0 0 10px rgba(0,0,0,0.6);border: solid 1px #255c71;border-radius: 5px 5px 0 0;background-color: #9eacb4"
         id="daochucase">
        <div style="height: 5px;">请选择导出测试用例的项目</div>
        <form method="POST" action="{{ url_for('case.daochucase') }}">
            <div style="background-color: whitesmoke;margin-top: 24px">
                选择导出的项目:
                <select name="interface_type">
                    {% for project in projects %}
                        <option name="project">{{ project.project_name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div style="margin-top: 50%;margin-bottom: auto">
                <input value="导出" type="submit" class="btn-default">
                <input value="取消" type="button" class=" btn-default" onclick="closeone()">
            </div>
        </form>
    </div>
    <div style="display:none;position:fixed;z-index: 210;width: 800px;top: 100px;left: 20%;box-shadow: 0 0 10px rgba(0,0,0,0.6);border: solid 1px #255c71;border-radius: 5px 5px 0 0;background-color: antiquewhite;"
         id="showonecase">
        <div style="height: 5px;">测试用例调试结果详情</div>
        <button class="close" onclick="closeonecase()">关闭</button>
        <div>
            <table class="" style="width:90%;table-layout:fixed;margin-top: 20px;">
                <thead>
                <tr>
                    <th>测试结果</th>
                    <th>测试日期</th>
                    <th>测试环境</th>
                    <th>接口耗时</th>
                </tr>
                </thead>
                <tbody id="testresult">
                </tbody>
            </table>
        </div>
        <div style="margin-top: 5%;margin-bottom: auto">
            <input value="取消" type="button" class=" btn-default" onclick="closeonecase()">
        </div>
    </div>
{% endblock %}
{% block js %}
    <script language="JavaScript">
        window.onload = getprotest();

        function testcase(id) {
            var divshow = document.getElementById('ceshiyongli');
            var divcase = document.getElementById('zhixingceshiyongli');
            divcase.name = id;
            divshow.style.display = 'block';
            $.ajax({
                url: '{{ url_for('getprojects') }}',
                type: 'POST',
                data: id,
                contentType: 'application/json; charset=UTF-8',
                processData: false,
                success: function (testyong) {
                    if (testyong['code'] == 200) {
                        var pro = document.getElementById('ceshiyongprco');
                        pro.innerText = testyong['data'];
                        xuanze = testyong['huanjing']
                        var xuanz = $('#xuanzehuanjing');
                        xuanz.empty();
                        for (var i = 0; i < xuanze.length; i++) {
                            var url = xuanze[i];
                            xuanz.append("<option id='ceshiurls'>  " + url + "</option>");
                        }
                    }
                    else {
                        window.location.reload();
                        alert(testyong['msg'])
                    }
                }, error: function (res) {
                    alert('异步请求失败，请联系管理员！！！');
                    window.location.reload();
                }
            });

        }

        function guanbi() {
            var divshow = document.getElementById('ceshiyongli');
            divshow.style.display = 'none';
        }

        function ceshiyongli() {
            var me = document.getElementById("xuanzehuanjing");
            var index = document.getElementById("xuanzehuanjing").selectedIndex;
            var url = me.options[index];
            var case_id = document.getElementById('zhixingceshiyongli');
            var data = {};
            if (url == undefined) {
                url = '';
            }
            else {
                url = url.text;
            }
            data['caseid'] = case_id.name;
            data['url'] = url;
            $.ajax({
                url: '{{ url_for('case.makeonlyonecase') }}',
                type: 'POST',
                data: JSON.stringify(data),
                contentType: 'application/json; charset=UTF-8',
                processData: false,
                success: function (result) {
                    if (result['code'] == 200) {
                        window.location.reload();
                        alert('测试用例在环境' + url + '中测试通过！！！');
                    }
                    else {
                        alert(result['msg'])
                        window.location.reload();
                    }
                }
            })
        };

        function getprotest() {
            var obj = document.getElementById('projecttestcase');
            var index = obj.selectedIndex;
            var value = obj.options[index].value;
            var interfacetest = document.getElementById('interfacetype');
            var inde = interfacetest.selectedIndex;
            var valu = interfacetest.options[inde].value;
            var data = {};
            data['project'] = value;
            data['interface_type'] = valu;
            var dddss = $('#ceshice');
            var inte = $('#interfacete');
            $.ajax({
                url: '{{ url_for('case.ser_yongli') }}',
                type: 'POST',
                data: JSON.stringify(data),
                contentType: 'application/json; charset=UTF-8',
                processData: false,
                success: function (data) {
                    if (data['code'] == 200) {
                        var mdata = data['data'];
                        var insertss = $('#testyonglis');
                        insertss.empty();
                        dddss.empty();
                        if (data['typeinter'] == 'http') {
                            inte.empty();
                            inte.append(" <th></th><th>编号</th><th width='10px'>项目</th><th>模块</th><th>所属接口</th><th>请求头</th><th>请求方式</th><th>参数</th><th>预期</th><th width='1%'>操作</th>")
                        } else if (data['typeinter'] == 'dubbo') {
                            inte.empty();
                            inte.append(" <th></th><th>编号</th><th width='10px'>项目</th><th>模块</th><th>所属接口</th><th>接口名称</th><th>请求方式</th><th>参数</th><th>预期</th><th width='1%'>操作</th>")
                        }
                        for (var m = 0; m < data['url'].length; m++) {
                            dddss.append('<option id=' + data['url'][m].id + ' >' + data['url'][m].url + '</option>')
                        }
                        ;
                        if (mdata.length < 1) {
                            insertss.append("<tr><td><h5><strong style='color:red'>没有找到接口用例</strong></h5><td></tr>")
                        }

                        else for (var i = 0; i < mdata.length; i++) {
                            if (mdata[i].Interface_is_tiaoshi == 0) {
                                insertss.append("<tr id='" + mdata[i].id + "'><td><input type='checkbox' name='yongli' value='" + mdata[i].id + "'></td><td>" + mdata[i].bianhao + "</td><td>" + mdata[i].project + "</td><td>" + mdata[i].model + "</td><td>" + mdata[i].interface + "</td><td >" + mdata[i].Interface_headers + "</td><td>" + mdata[i].Interface_meth + "</td><td>" + mdata[i].Interface_pase + "</td><td>" + mdata[i].Interface_assert + "</td><td><div class='am-btn-toolbar'><div class='am-btn-group am-btn-group-xs'><a href='/edit_case/" + mdata[i].id + "'><input type='button' value='编辑' class='btn btn-primary'></a> <a onclick='deletcase(" + mdata[i].id + ")'><input type='button' value='删除'class='btn btn-danger'></a><input type='button' value='调试' class='btn btn-default' id='" + mdata[i].id + "' onclick='testcase(this.id)'><input type='button' value='日志' class='btn btn-default' id='" + mdata[i].id + "' onclick='caseshowone(this.id)'></div></div></td></tr>")
                            }
                            else if (mdata[i].Interface_is_tiaoshi == 1 && mdata[i].Interface_tiaoshi_shifou == 1) {
                                insertss.append("<tr  id='" + mdata[i].id + "'><td><input type='checkbox' name='yongli' value='" + mdata[i].id + "'></td><td>" + mdata[i].bianhao + "</td><td>" + mdata[i].project + "</td><td>" + mdata[i].model + "</td><td>" + mdata[i].interface + "</td><td >" + mdata[i].Interface_headers + "</td><td>" + mdata[i].Interface_meth + "</td><td>" + mdata[i].Interface_pase + "</td><td>" + mdata[i].Interface_assert + "</td><td><div class='am-btn-toolbar'><div class='am-btn-group am-btn-group-xs'><a href='/edit_case/" + mdata[i].id + "'><input type='button' value='编辑' class='btn btn-primary'></a> <a onclick='deletcase(" + mdata[i].id + ")'><input type='button' value='删除'class='btn btn-danger'></a><input type='button' value='调试' class='btn btn-danger' id='" + mdata[i].id + "' onclick='testcase(this.id)'><input type='button' value='日志' class='btn btn-default' id='" + mdata[i].id + "' onclick='caseshowone(this.id)'></div></div></td></tr>")
                            } else if (mdata[i].Interface_is_tiaoshi == 1 && mdata[i].Interface_tiaoshi_shifou == 0) {
                                insertss.append("<tr  id='" + mdata[i].id + "'><td><input type='checkbox' name='yongli' value='" + mdata[i].id + "'></td><td>" + mdata[i].bianhao + "</td><td>" + mdata[i].project + "</td><td>" + mdata[i].model + "</td><td>" + mdata[i].interface + "</td><td >" + mdata[i].Interface_headers + "</td><td>" + mdata[i].Interface_meth + "</td><td>" + mdata[i].Interface_pase + "</td><td>" + mdata[i].Interface_assert + "</td><td><div class='am-btn-toolbar'><div class='am-btn-group am-btn-group-xs'><a href='/edit_case/" + mdata[i].id + "'><input type='button' value='编辑' class='btn btn-primary'></a> <a onclick='deletcase(" + mdata[i].id + ")'><input type='button' value='删除'class='btn btn-danger'></a><input type='button' value='调试' class='btn btn-success' id='" + mdata[i].id + "' onclick='testcase(this.id)'><input type='button' value='日志' class='btn btn-default' id='" + mdata[i].id + "' onclick='caseshowone(this.id)'></div></div></td></tr>")
                            }
                        }
                        ;
                    }
                }
                , error: function (res) {
                    alert('异步请求失败，请联系管理员！！！');
                    window.location.reload();
                }
            })
        }

        function closeone() {
            var divshow = document.getElementById('daochucase');
            divshow.style.display = 'none';
        }

        function dakai() {
            var divshow = document.getElementById('daochucase');
            divshow.style.display = 'block';
        }

        function closeonecase() {
            var divshow = document.getElementById('showonecase');
            divshow.style.display = 'none';
        }

        function caseshowone(id) {
            var divshow = document.getElementById('showonecase');
            divshow.style.display = 'block';

            $.ajax({
                url: '{{ url_for('case.caseonedeteil') }}',
                type: 'POST',
                data: id,
                contentType: 'application/json; charset=UTF-8',
                processData: false,
                success: function (data) {
                    var inte = $('#testresult');
                    if (data['code'] == 200) {
                        inte.empty();
                        for (var i = 0; i < data['data'].length; i++) {
                            inte.append("<tr><td>" + data['data'][i]['result'] + "</td><td>" + data['data'][i]['date'] + "</td><td>" + data['data'][i]['event'] + "</td><td>" + data['data'][i]['spend'] + "</td></tr>")
                        }
                    }
                    else {
                        inte.empty();
                        inte.append("<h1>" + data['messgage'] + "</h1>")
                    }
                }
                , error: function (res) {
                    alert('异步请求失败，请联系管理员！！！');
                    window.location.reload();
                }
            })
        }

        function deletcase(id) {
            $.ajax({
                url: '{{url_for( 'home.yongli') }}',
                type: 'DELETE',
                data: id,
                contentType: 'application/json; charset=UTF-8',
                processData: false,
                success: function (data) {
                    if (data['code'] != 2) {
                        alert(data['data'])
                    } else {
                        alert(data['data'])
                        caseid = document.getElementById(id);
                        caseid.hidden = true;
                    }
                }
            })
        }
    </script>
{% endblock %}
